var app = getApp();
Page({
    data: {
        userInfo: app.accountInfo(),
        canIUseGetUserProfile: false,
    },
    onLoad() {
        var that = this;
        if (wx.getUserProfile) {
            var info = app.accountInfo();
            that.setData({
                canIUseGetUserProfile: true,
                userInfo: info
            })
        }
    },
    toInfo() {
        wx.reLaunch({
            url: '../user/user',
        })
    },
    getUserProfile(e) {
        var that = this;
        wx.getUserProfile({
            desc: '用于完善会员资料',
            success: function (profileres) {
                wx.login({
                    success(loginres) {
                        //登录请求开始
                        wx.request({
                            url: app.rootUrl + '/user/authWx',
                            data: {
                                'code': loginres.code,
                                'encryptedData': profileres.encryptedData,
                                'iv': profileres.iv
                            },
                            header: {
                                'Content-Type': 'application/x-www-form-urlencoded',
                                //'Accept': 'application/json;charset=utf-8',
                            },
                            method: 'POST',
                            success: function (res) {
                                wx.hideLoading();
                                if (res.data.status == 500) {
                                    wx.showToast({
                                        title: res.data.msg,
                                        icon: 'none',
                                    })
                                    return false;
                                } else if (res.data.status == 1) {
                                    wx.showModal({
                                        title: '请绑定账号',
                                        content: '',
                                        editable: true,
                                        placeholderText:'请填写学号/工号',
                                        cancelText: '拒绝绑定',
                                        confirmText: '提交绑定',
                                        success(mres) {
                                            //user/authBind
                                            //String number, String openId, String nick, String logo, Integer type
                                            if (mres.confirm) {
                                                if(!mres.content){
                                                   wx.showToast({
                                                      title: '请输入',
                                                      icon: 'none',
                                                    })
                                                    return false
                                                }
                                                res.data.data['accountNumber'] = mres.content
                                                wx.request({
                                                  url: app.rootUrl + '/user/authBind',
                                                  data: res.data.data,
                                                  header: {
                                                    'Content-Type': 'application/json',
                                                  },
                                                  method: 'POST',
                                                  success: function (authres) {
                                                    wx.showToast({
                                                      title: authres.data.msg,
                                                      icon: 'none',
                                                    })
                                                    if (authres.data.status == 0) {
                                                      wx.setStorage({
                                                        key: "accountInfo",
                                                        data: authres.data.data,
                                                    });
                                                    setTimeout(function () {
                                                        wx.reLaunch({
                                                            url: '../user/user',
                                                        })
                                                    }, 900);
                                                    }
                                                  }
                                                })
                                            } else if (mres.cancel) {
                                                setTimeout(function () {
                                                  wx.reLaunch({
                                                      url: '../user/user',
                                                  })
                                              }, 900);
                                            }
                                        }
                                    })
                                } else {
                                    wx.showToast({
                                        title: '登录成功',
                                        icon: 'none',
                                    })
                                    wx.setStorage({
                                        key: "accountInfo",
                                        data: res.data.data,
                                    });
                                    setTimeout(function () {
                                        wx.reLaunch({
                                            url: '../user/user',
                                        })
                                    }, 900);
                                }
                            },
                            fail: function (res) {
                                wx.showToast({
                                    title: res.errMsg,
                                    icon: 'none',
                                })
                            }
                        })
                    }
                })
            }
        })
    },
})